import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { GameController } from "../mvc/GameController";
import { GuidePetSkillRefreshRule } from "../rules/GuidePetSkillRefreshRule";
import { DataEvent } from "../events/DataEvent";
import { GuideTipsManager } from "../manager/GuideTipsManager";
import { ItemData } from "../resource/info/ItemData";
//class GuidePetSkillRefreshController
    
    export  class GuidePetSkillRefreshController extends Controller
    {
       

        /* internal  */initListener(): void
        {
            Dispatcher.addEventListener(EventName.PetWindowClose, this.onPetModuleCloseHandler);
            Dispatcher.addEventListener(EventName.PetWindowShow, this.onPetModuleShowHandler);
            Dispatcher.addEventListener(EventName.PetTabBarToSkill, this.onPetTabBarToSkillHandler);
            Dispatcher.addEventListener(EventName.PetSkillWindowShow, this.onPetSkillWindowShowHandler);
            Dispatcher.addEventListener(EventName.PetSkillWindowClose, this.onPetSkillWindowCloseHandler);
            Dispatcher.addEventListener(EventName.PetSkillRefressResult, this.onPetSkillRefreshResultHandler);
            Dispatcher.addEventListener(EventName.PetSkillGetResult, this.onPetSkillGetResultHandler);
            return;
        }

        /* internal  */removeListener(): void
        {
            Dispatcher.removeEventListener(EventName.PetWindowClose, this.onPetModuleCloseHandler);
            Dispatcher.removeEventListener(EventName.PetWindowShow, this.onPetModuleShowHandler);
            Dispatcher.removeEventListener(EventName.PetTabBarToSkill, this.onPetTabBarToSkillHandler);
            Dispatcher.removeEventListener(EventName.PetSkillWindowShow, this.onPetSkillWindowShowHandler);
            Dispatcher.removeEventListener(EventName.PetSkillWindowClose, this.onPetSkillWindowCloseHandler);
            Dispatcher.removeEventListener(EventName.PetSkillRefressResult, this.onPetSkillRefreshResultHandler);
            Dispatcher.removeEventListener(EventName.PetSkillGetResult, this.onPetSkillGetResultHandler);
            NetDispatcher.removeCmdListener(ServerCommand.BackpackDataChange, this.onBackPackItemsChange);
            return;
        }

        /* internal  */openPetModule(): void
        {
            if (GameController.pet.petModule.isHide) 
            {
                cache.guidePetSkillRefresh.guideStep = GuidePetSkillRefreshRule.StepOpenPetModule;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetSkillRefreshOpenModule));
            }
            else 
            {
                this.openSkillPanel();
            }
            return;
        }

        /* internal  */openSkillPanel(): void
        {
            cache.guidePetSkillRefresh.guideStep = GuidePetSkillRefreshRule.StepOpenSkillPanel;
            GuideTipsManager.hideGuideTips();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetSkillRefreshOpenSkill));
            return;
        }

        /* internal  */useSkillStone(): void
        {
            cache.guidePetSkillRefresh.guideStep = GuidePetSkillRefreshRule.StepUseSkillStone;
            GuideTipsManager.hideGuideTips();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetSkillRefreshUseItem));
            return;
        }

        /* internal  */refreshSkill(): void
        {
            cache.guidePetSkillRefresh.guideStep = GuidePetSkillRefreshRule.StepRefreshSkill;
            GuideTipsManager.hideGuideTips();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetSkillRefreshOpt));
            return;
        }

        /* internal  */refreshSkill2(): void
        {
            cache.guidePetSkillRefresh.guideStep = GuidePetSkillRefreshRule.StepRefreshSkill;
            GuideTipsManager.hideGuideTips();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetSkillRefreshOpt2));
            return;
        }

        /* internal  */callSkill(): void
        {
            cache.guidePetSkillRefresh.guideStep = GuidePetSkillRefreshRule.StepCallSkill;
            GuideTipsManager.hideGuideTips();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetSkillCallOpt));
            return;
        }

        /* internal  */onGuidePetSkillRefresh(arg1: DataEvent=null): void
        {
            this.initListener();
            this.openPetModule();
            return;
        }

        /* internal  */onPetTabBarToSkillHandler(arg1: DataEvent): void
        {
            var loc1=false;
            if (cache.guidePetSkillRefresh.needOpenSkillPanel()) 
            {
                loc1 = Boolean(arg1.data);
                if (loc1) 
                {
                    this.useSkillStone();
                }
                else 
                {
                    this.openSkillPanel();
                }
            }
            return;
        }

        /* internal  */onPetModuleCloseHandler(arg1: DataEvent): void
        {
            if (cache.guidePetSkillRefresh.needOpenSkillModule()) 
            {
                this.openPetModule();
            }
            return;
        }

        /* internal  */onPetModuleShowHandler(arg1: DataEvent): void
        {
            if (cache.guidePetSkillRefresh.guideStep == GuidePetSkillRefreshRule.StepOpenPetModule) 
            {
                this.openSkillPanel();
            }
            return;
        }

        /* internal  */onPetSkillWindowShowHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (cache.guidePetSkillRefresh.guideSkillRefresh()) 
            {
                loc1 = arg1.data as ItemData;
                if (loc1 && cache.guidePetSkillRefresh.isGuideItem(loc1)) 
                {
                    if (cache.guidePetSkillRefresh.needOpenSkillModule()) 
                    {
                        if (cache.guidePetSkillRefresh.refreshEd2) 
                        {
                            this.callSkill();
                        }
                        else if (cache.guidePetSkillRefresh.refreshEd) 
                        {
                            this.refreshSkill2();
                        }
                        else 
                        {
                            this.refreshSkill();
                        }
                    }
                }
                else 
                {
                    this.openPetModule();
                }
            }
            return;
        }

        /* internal  */onPetSkillWindowCloseHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (loc1 && cache.guidePetSkillRefresh.isGuideItem(loc1)) 
            {
                if (cache.guidePetSkillRefresh.needSkillRefreshPanel()) 
                {
                    this.openPetModule();
                }
            }
            return;
        }

        /* internal  */onPetSkillRefreshResultHandler(arg1: DataEvent): void
        {
            var loc1=false;
            if (cache.guidePetSkillRefresh.needSkillRefreshPanel()) 
            {
                loc1 = Boolean(arg1.data);
                if (loc1) 
                {
                    cache.guidePetSkillRefresh.refreshEd = true;
                    this.refreshSkill2();
                }
                else 
                {
                    cache.guidePetSkillRefresh.refreshEd2 = true;
                    this.callSkill();
                }
            }
            return;
        }

        /* internal  */onPetSkillGetResultHandler(arg1: DataEvent): void
        {
            if (cache.guidePetSkillRefresh.guideSkillRefresh()) 
            {
                cache.guidePetSkillRefresh.guideStep = -1;
                GuideTipsManager.hideGuideTips();
                this.removeListener();
            }
            return;
        }

        /* internal  */onBackPackItemsChange(arg1: Object): void
        {
            var loc2=0;
            var loc1=cache.pack.backPackCache.getItemByCode(cache.guidePetSkillRefresh.getGuideItemCode());
            if (!(loc1 == null) && !(loc1.itemExInfo == null)) 
            {
                if (loc1.itemExInfo.hasOwnProperty("rd_g_step")) 
                {
                    loc2 = loc1.itemExInfo.rd_g_step;
                    var loc3=loc2;
                    switch (loc3) 
                    {
                        case 1:
                        {
                            break;
                        }
                        case 2:
                        {
                            cache.guidePetSkillRefresh.refreshEd = true;
                            break;
                        }
                        case 3:
                        {
                            cache.guidePetSkillRefresh.refreshEd2 = true;
                            break;
                        }
                    }
                }
            }
            if (loc1 == null) 
            {
                NetDispatcher.removeCmdListener(ServerCommand.BackpackDataChange, this.onBackPackItemsChange);
            }
            else 
            {
                this.initListener();
                flash.utils.setTimeout(this.onGuidePetSkillRefresh, 1000);
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.Guide_PetSkillRefresh, this.onGuidePetSkillRefresh);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onBackPackItemsChange);
            return;
        }
    }
